package io.github.uicdb.et.mixin;

import io.github.uicdb.et.ModHelper;
import io.github.uicdb.et.ModItems;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;

@Mixin(FoodData.class)
public class FoodDataMixin {
    @Shadow
    private int foodLevel;

    @Shadow
    private int lastFoodLevel;

    @Inject(method = "tick", at = @At("TAIL"))
    private void et_tick_head(Player player, CallbackInfo ci) {
        if (player.getRandom().nextFloat() < 0.25) {
            ModHelper.findCurios(player, i -> i.is(ModItems.WHEAT_WAVE.asItem()), this::et_hackFoodLevel);
        }
    }

    private void et_hackFoodLevel(LivingEntity entity, ICuriosItemHandler iCuriosItemHandler) {
        if (this.foodLevel < this.lastFoodLevel) {
            this.foodLevel = lastFoodLevel;
        }
    }
}
